﻿﻿/**
 * Author: Jun Li
 * Name: init.js
 * Version: $Revision: 1.0.0 $, $Date: 2008/12/19 $
 * Description: 该JavaScript是管理框架的核心
 */

var REFRESH_COUNTER = 0;
// 屏蔽错误,当出现脚本错误时刷新页面
function killErrors() {
	//window.location.reload();
	return false;
}
window.onerror = killErrors;

/**
 * 定义全局变量
 */
var JS_ID_2008="JavaScript_ID_2008";
var JS_COUNT = 0; // JavaScript Counter
var CSS_COUNT = 0; // Css Counter
var CURRENT_PAGE = "";

/**
 * 浏览器兼容性处理
 */
var lBrowser = {};
lBrowser.agt = navigator.userAgent.toLowerCase();
lBrowser.isW3C = document.getElementById ? true:false;
lBrowser.isIE = ((lBrowser.agt.indexOf("msie") != -1) && (lBrowser.agt.indexOf("opera") == -1) && (lBrowser.agt.indexOf("omniweb") == -1));
lBrowser.isNS6 = lBrowser.isW3C && (navigator.appName=="Netscape") ;
lBrowser.isOpera = lBrowser.agt.indexOf("opera") != -1;
lBrowser.isGecko = lBrowser.agt.indexOf("gecko") != -1;
lBrowser.ieTrueBody =function (){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
};

if(lBrowser.isNS6){
    //firefox innerText define
    HTMLElement.prototype.__defineGetter__( "innerText",
        function(){
            return this.textContent;
        });
    HTMLElement.prototype.__defineSetter__( "innerText",
        function(sText){
            this.textContent=sText;
        });
}

/**
 * 屏蔽鼠标右键
 */
document.oncontextmenu = function(){
    return false;
}

var Jpxx = {};

/**
 * 窗口最大化
 */
Jpxx.resize = function(){
    window.moveTo(0,0) ;
    window.resizeTo(screen.availWidth,screen.availHeight); 
}

/**
 * 初始化函数，核心函数
 */
Jpxx.init = function (){    
    /**
     * Load DWR
     */
    try{
        loadJS("../dwr/engine.js");
        loadJS("../dwr/util.js");
    }catch(e){
        alert("DWR FRAMEWORK DEPLOY ERROR! "+e.message);
        //alert("DWR FRAMEWORK DEPLOY ERROR!");
        return;
    }
    loadJS("js/util.js");
    
    /**
	 * URL Checking
	 */
    var url = window.location.href;
    if(url){
        var page = url.substring(url.lastIndexOf('/')+1);
        CURRENT_PAGE = page;
        if(page.startWith("login.htm")){
            loginPage();
        }else if(page.startWith("index.htm")){
            indexPage();
        }
    }

}
/**
 *******************************************************************************
 * Page Operation
 *******************************************************************************
 */
function loginPage(){
	Jpxx.resize();
	this.loadCSS("style/login.css");    
    loadJS("../dwr/interface/admin_login.js");
    loadJS("js/transaction/login.js");
    admin_login.getLoginHtml(function(data){
        var body = document.getElementsByTagName('body').item(0);
        var o = createDivElement("LOGIN_HTML");
        o.innerHTML=data;
        body.appendChild(o);
    });
}

function indexPage(){
	
}
/**
 *******************************************************************************
 * JavaScript Operation: Load JavaScript File
 *******************************************************************************
 */
function loadJS(url,charset){
    var oXmlHttp = getXMLHttpRequest();
    //同步传输模式
    oXmlHttp.open('GET', url, false);
    oXmlHttp.send(null);
    if (oXmlHttp.status == 200) {
        appendJS(oXmlHttp.responseText,charset);
    }
    else{
        alert( 'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')'+url ) ;
    }
}

function appendJS(source,charset){
    var id = JS_ID_2008+"_"+(new Date().getTime()+"_"+JS_COUNT);
    if (source != null){
        var oHead = document.getElementsByTagName('HEAD').item(0);
        var oScript = document.createElement("script");
        oScript.language = "javascript";
        oScript.type = "text/javascript";
        oScript.id = id;
        if(charset){
            oScript.charset = charset;
        }
        oScript.defer = true;
        oScript.text = source;
        oHead.appendChild(oScript);
        JS_COUNT++;
    }
}

var getXMLHttpRequest = function() {
    var xmlreq = false;
    if (window.XMLHttpRequest) {
        xmlreq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e1) {
            try {
                xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {
            }
        }
    }
    return xmlreq;
}
/**
 *******************************************************************************
 * CSS Operation: Load CSS File
 *******************************************************************************
 */
 var loadCSS = function(strCssFile){
 	var HeadElement = document.getElementsByTagName("HEAD")[0];
    var cssFileID   = "__CssFile__"+this.CSS_COUNT;
    while(document.getElementById(cssFileID) != null) {
        try {
            HeadElement.removeChild(document.getElementById(cssFileID));
        }
        catch(ex)
        {}
    }
    with(HeadElement.appendChild(document.createElement("LINK"))){
        id = cssFileID; href = strCssFile; rel = "stylesheet"; type = "text/css";
        }
    CSS_COUNT++;
}

var createDivElement = function(id,style) {
    var div = document.createElement("div");
    div.id = id;
    if(style){
        div.style = style;
    }
    return div;
}

/**
 * 初始化
 */
window.onload = Jpxx.init;